package com.sms.base.eve.smsbaseeve.controller;

import com.alibaba.fastjson2.JSON;
import com.sms.base.eve.smsbaseeve.domain.AlarmPerimeterDev;
import com.sms.base.eve.smsbaseeve.domain.AlarmPerimeterRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("jdbc")
public class JDBCController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/alarmPerimeterDev/list")
    @SuppressWarnings("all")
    public Map<String, Object> queryByPageDev(AlarmPerimeterDev alarmPerimeterDev) {
        log.info("alarmPerimeterDev 查询参数:{}", JSON.toJSONString(alarmPerimeterDev));
        Map<String, Object> hashMap = new HashMap<>();

        String sql = "SELECT count(1) FROM alarm_perimeter_dev";
        int count = jdbcTemplate.queryForObject(sql, Integer.class);
        hashMap.put("total", count);
        hashMap.put("code", 200);
        hashMap.put("msg", "查询成功");

        int pageNum = alarmPerimeterDev.getPageNumber();
        int pageSize = alarmPerimeterDev.getPageSize();

        String sql1 = "SELECT * FROM alarm_perimeter_dev limit " + pageNum + "," + pageSize;
        List query = jdbcTemplate.query(sql1, new BeanPropertyRowMapper(AlarmPerimeterDev.class));
        log.info("alarmPerimeterDev 结果query:{}", JSON.toJSONString(query));
        hashMap.put("rows", query);

        log.info("alarmPerimeterDev 结果:{}", JSON.toJSONString(hashMap));

        return hashMap;
    }

    @GetMapping("/alarmPerimeterRecord/list")
    @SuppressWarnings("all")
    public Map<String, Object> queryByPageRecord(AlarmPerimeterRecord alarmPerimeterRecord) {
        log.info("alarmPerimeterRecord 查询参数:{}", JSON.toJSONString(alarmPerimeterRecord));
        Map<String, Object> hashMap = new HashMap<>();

        String sql = "SELECT count(1) FROM alarm_perimeter_record";
        int count = jdbcTemplate.queryForObject(sql, Integer.class);
        hashMap.put("total", count);
        hashMap.put("code", 200);
        hashMap.put("msg", "查询成功");

        int pageNum = alarmPerimeterRecord.getPageNumber();
        int pageSize = alarmPerimeterRecord.getPageSize();

        String sql1 = "SELECT * FROM alarm_perimeter_record limit " + pageNum + "," + pageSize;
        List query = jdbcTemplate.query(sql1, new BeanPropertyRowMapper(AlarmPerimeterRecord.class));
        log.info("alarmPerimeterRecord 结果query:{}", JSON.toJSONString(query));
        hashMap.put("rows", query);

        log.info("alarmPerimeterRecord 结果:{}", JSON.toJSONString(hashMap));

        return hashMap;
    }

}
